package com.vdongchina.thread.executor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.*;

public class ExecutorTest {

    private static final Logger logger = LoggerFactory.getLogger(ExecutorTest.class);


    public static void main(String[] args) throws ExecutionException, InterruptedException {

        executorServiceTest();

        scheduledExecutorService();

        logger.info("主线程结束!");
    }


    public static void executorServiceTest() throws ExecutionException, InterruptedException {

        //缓存内存线程池
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
        Future<?> futureRunnable = cachedThreadPool.submit(new ThreadRunnable("线程任务0"));
        logger.info((String) futureRunnable.get());

        Future<String> futureCallable = cachedThreadPool.submit(new ThreadCallable("线程任务0"));
        logger.info("======>");
        logger.info("future的返回值会阻塞主线程继续执行:{}", (String) futureCallable.get());


        //futureTask的使用, 还未知,有待去理解
        java.util.concurrent.FutureTask futureTask = new java.util.concurrent.FutureTask(new ThreadCallable("线程任务3"));
        cachedThreadPool.execute(futureTask);
        logger.info("======>");
        logger.info("future的返回值不会阻塞主线程继续执行:{}", futureTask.get());
        cachedThreadPool.shutdown();

        //固定线程池
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
        fixedThreadPool.execute(new ThreadRunnable("线程任务 1"));
        fixedThreadPool.shutdown();

        //单线程,多用于活动处理
        ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
        singleThreadExecutor.execute(new ThreadRunnable("线程任务 2"));
        singleThreadExecutor.shutdown();

    }

    public static void scheduledExecutorService() {
        ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(3);
        //10秒后执行,主线程不会被阻塞
        scheduledThreadPool.schedule(new ThreadRunnable("线程执行0"), 10, TimeUnit.SECONDS);
        scheduledThreadPool.shutdown();

        ScheduledExecutorService scheduledExecutorPool = Executors.newSingleThreadScheduledExecutor();
        ScheduledFuture scheduledFuture = scheduledExecutorPool.schedule(new ThreadCallable("线程执行1"), 5, TimeUnit.SECONDS);
        scheduledExecutorPool.shutdown();

    }


    /**
     * 无返回值
     */
    static class ThreadRunnable implements Runnable {
        private String name;

        public ThreadRunnable(String name) {
            this.name = name;
        }

        @Override
        public void run() {
            logger.info("[线程执行-{}, 线程自命名-{}]", name, Thread.currentThread().getName());
        }
    }


    /**
     * 有返回值
     */
    static class ThreadCallable implements Callable {

        private String name;

        public ThreadCallable(String name) {
            this.name = name;
        }

        @Override
        public Object call() throws Exception {
            Thread.sleep(3000);
            logger.info("{} - 子线程休眠3秒", name);
            return name + "-" + Thread.currentThread().getName();
        }
    }


}
